package com.guiwu.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.format.DateTimeFormatter;

/**
 * Web配置类
 * 配置日期格式化和其他Web相关设置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LocalDateConverter localDateConverter;

    /**
     * 配置日期时间格式化器和转换器
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 注册日期时间格式化器
        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        registrar.registerFormatters(registry);

        // 注册自定义LocalDate转换器
        registry.addConverter(localDateConverter);
    }

    /**
     * 配置静态资源处理
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 确保静态资源可以正确访问
        registry.addResourceHandler("/icon.png")
                .addResourceLocations("classpath:/static/");

        // 配置favicon.ico映射到icon.png
        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/static/icon.png");

        // 配置其他静态资源
        registry.addResourceHandler("/css/**", "/js/**", "/images/**", "/static/**")
                .addResourceLocations("classpath:/static/");
    }
}
